package cn.elwy.rcp.ui.table.setting.listener;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TreeColumn;

import cn.elwy.rcp.ui.table.TablePane;
import cn.elwy.rcp.ui.table.model.ColumnInfo;
import cn.elwy.rcp.ui.table.util.ColumnVisibleUtil;

public class VisibleSettingListener extends SelectionAdapter {
  protected TablePane tablePane;

  @Override
  public void widgetSelected(SelectionEvent e) {
    MenuItem widget = (MenuItem) e.widget;
    Object item = widget.getData();
    boolean resizable = true;
    ColumnInfo columnInfo = (ColumnInfo) widget.getData("columnInfo");
    if (item instanceof TreeColumn) {
      TreeColumn column = (TreeColumn) item;
      resizable = !column.getResizable();
      ColumnVisibleUtil.setColumnVisible(column, resizable);
    } else {
      TableColumn column = (TableColumn) item;
      resizable = !column.getResizable();
      ColumnVisibleUtil.setColumnVisible(column, resizable);
    }
    tablePane.setConfigChanged(true);
    columnInfo.setVisible(resizable);
  }

  public TablePane getTablePane() {
    return tablePane;
  }

  public void setTablePane(TablePane tablePane) {
    this.tablePane = tablePane;
  }

}